package com.hmall.trade.listener;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.hmall.trade.domain.po.Order;
import com.hmall.trade.service.IOrderService;
import com.hmall.trade.service.MQConstants;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @create: 张曦文
 * 日期 :2025/11/6 17:37
 * 功能 :
 **/
@Component
@RequiredArgsConstructor
@Slf4j
public class PayStatusListener {
    private final IOrderService iOrderService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "trade.pay.success.queue"),
            exchange = @Exchange(name = "pay.direct",type = ExchangeTypes.DIRECT),
            key = "pay.success"
    ))
    public void find(Long orderId, Message message) {
        Object userId = message.getMessageProperties().getHeader("user-info");
        iOrderService.markOrderPaySuccess(orderId);
        System.out.println("更新成功");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MQConstants.DELAY_ORDER_QUEUE_NAME),
            exchange = @Exchange(name = MQConstants.DELAY_EXCHANGE_NAME,delayed = "true",type = ExchangeTypes.DIRECT),
            key = MQConstants.DELAY_ORDER_KEY
    ))
    public void find2(Long orderId) {
        //根据id获取订单
        Order order = iOrderService.getById(orderId);
        if(order==null){
            log.info("订单不存在");
            return;
        }
        if (order.getStatus()==1){
            order.setStatus(5);
            iOrderService.updateById(order);
        }
        System.out.println("===更新订单状态成功==");
    }
}
